package com.wlt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlt.pojo.Category;
import com.wlt.result.BaseResult;
import com.wlt.service.CategoryService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/category")
public class CategoryController
{
    @DubboReference
    private CategoryService categoryService;
    
    /**
     * 分页查询
     * @param page  页码
     * @param size  每页条数
     * @return      查询结果
     */
    @GetMapping("/search")
    public BaseResult<Page<Category>> search (int page, int size)
    {
        Page<Category> search = categoryService.search(page, size);
        
        return BaseResult.success(search);
    }
    
    /**
     * 新增广告
     * @param category  广告实体
     * @return          执行结果
     */
    @PostMapping ("/add")
    public BaseResult add (@RequestBody Category category)
    {
        categoryService.add(category);
        
        return BaseResult.success();
    }
    
    /**
     * 修改广告
     * @param category  广告实体
     * @return          执行结果
     */
    @PutMapping ("/update")
    public BaseResult update (@RequestBody Category category)
    {
        categoryService.update(category);
        
        return BaseResult.success();
    }
    
    /**
     * 修改广告状态
     * @param id        广告id
     * @param status    广告状态，0：未启用 1：启用
     * @return          执行结果
     */
    @PutMapping("/updateStatus")
    public BaseResult updateStatus (Long id, Integer status)
    {
        categoryService.updateStatus(id, status);
        
        return BaseResult.success();
    }
    
    /**
     * 根据id查询
     * @param id    广告id
     * @return      执行结果
     */
    @GetMapping ("/findById")
    public BaseResult<Category> findById (Long id)
    {
        Category byId = categoryService.findById(id);
        
        return BaseResult.success(byId);
    }
    
    /**
     * 批量删除广告
     * @param ids   广告id集合
     * @return      执行结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete (Long[] ids)
    {
        categoryService.delete(ids);
        
        return BaseResult.success();
    }
}
